package com.example.jones.test;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.util.CookbookImage;
//问题：收藏次数对应点击次数&评论次数对应分享次数
//每日推荐的显示
public class TopAdepter extends BaseAdapter {

	// 定义需要包装的JSONArray对象
	private JSONArray jsonArray;
    Handler hd;
	private Context ctx;

	public TopAdepter(Handler mhd, JSONArray jsonArray,Context ctx )
	{
    	hd=mhd;
		this.jsonArray = jsonArray;
		this.ctx = ctx;
    }

	public int getCount() {
		return 3;
	}

	public Object getItem(int position) {
		return jsonArray.optJSONObject(position);
	}

	public long getItemId(int position) {
		try{
			// 返回物品的ID
			return ((JSONObject)getItem(position)).getInt("id");
		}
		catch (JSONException e){
			e.printStackTrace();
		}
		return 0;
	}

	public View getView( int pot, View convertView, ViewGroup parent) {
		System.out.println("##################TopAdepter：getView"+pot);
		
		convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.top_list_item, null);
	
		ImageView iv_collect=(ImageView) convertView.findViewById(R.id.iv_collect);
		ImageView iv_comment=(ImageView) convertView.findViewById(R.id.iv_comment);
		ImageView icon=(ImageView) convertView.findViewById(R.id.icon);
		TextView iv_collect_num=(TextView) convertView.findViewById(R.id.iv_collect_num);
		TextView iv_comment_num=(TextView) convertView.findViewById(R.id.iv_comment_num);

		
		//获取JSONArray数组元素的property属性
		try {
			iv_collect_num.setText(((JSONObject)getItem(pot)).getString("dishHits"));
			iv_comment_num.setText(((JSONObject)getItem(pot)).getString("shareNum"));
			
			CookbookImage cbimg=new CookbookImage();
			String dishImage=((JSONObject)getItem(pot)).getString("dishImage");
			icon.setImageBitmap(cbimg.getmybitmap(dishImage));
			
		} catch (JSONException e) {
			System.out.println("########TopAdepter：getView 获取JSONArray数组元素的属性出错！");
			e.printStackTrace();
		}

		
		iv_comment.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				hd.sendEmptyMessage(0);
			}
		});
		
		iv_collect.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				hd.sendEmptyMessage(1);
			}
		});
		
		final int ifp = pot;
		icon.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					Intent in=new Intent();
					in.putExtra("cbID", ((JSONObject)getItem(ifp)).getString("cookbookID"));
					in.putExtra("dishImage", ((JSONObject)getItem(ifp)).getString("dishImage"));
					in.putExtra("dishName", ((JSONObject)getItem(ifp)).getString("dishName"));
					in.setClass(ctx, DtlActivity.class);
					ctx.startActivity(in);
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		});
		return convertView;
	} 

}
